<template>
  <view class="container">
    <!-- 导航栏 -->
    <view class="nav-bar">
      <view class="nav-title">消息</view>
      <view class="nav-action" @click="markAllRead">
        <image src="/static/icons/read-all.png" mode="aspectFit" class="action-icon" />
        <text>标记全部已读</text>
      </view>
    </view>

    <!-- 消息分类 -->
    <view class="tabs">
      <view class="tab active" @click="switchTab('all')">
        <text>全部</text>
        <view class="tab-indicator"></view>
      </view>
      <view class="tab" @click="switchTab('user')">
        <text>用户消息</text>
      </view>
      <view class="tab" @click="switchTab('system')">
        <text>系统通知</text>
      </view>
    </view>

    <!-- 消息列表 - 优化居中布局 -->
    <scroll-view class="message-list-container" scroll-y>
      <view class="message-list-wrapper">
        <!-- 系统通知 - 审核通过 -->
        <view 
          class="message-card system-notice" 
          @click="goToChat('system_1')"
        >
          <view class="message-header">
            <view class="avatar">
              <image
                src="https://ai-public.mastergo.com/ai/img_res/b7f8ff0264c28a0e73b20aa75d866bb1.jpg"
                mode="aspectFill"
              />
            </view>
            <view class="message-info">
              <view class="message-title">
                <text class="sender">校园失物招领</text>
                <text class="time">10:30</text>
              </view>
              <view class="message-content">
                <text>您的失物招领信息已通过审核</text>
                <view class="badge">审核</view>
              </view>
            </view>
            <view class="status-indicator">
              <text class="status-text">已读</text>
            </view>
          </view>
        </view>

        <!-- 用户消息 - 找到物品 -->
        <view 
          class="message-card unread" 
          @click="goToChat('user_1')"
        >
          <view class="message-header">
            <view class="avatar">
              <image
                src="https://ai-public.mastergo.com/ai/img_res/37998c4797d59d14cf4d1abad6197464.jpg"
                mode="aspectFill"
              />
            </view>
            <view class="message-info">
              <view class="message-title">
                <text class="sender">张三同学</text>
                <text class="time">09:45</text>
              </view>
              <view class="message-content">
                <text>您好，我找到了您丢失的校园卡，请联系我领取</text>
              </view>
            </view>
            <view class="status-indicator">
              <view class="unread-dot"></view>
            </view>
          </view>
        </view>

        <!-- 用户消息 - 物品认领 -->
        <view 
          class="message-card" 
          @click="goToChat('user_2')"
        >
          <view class="message-header">
            <view class="avatar">
              <image
                src="https://ai-public.mastergo.com/ai/img_res/db5b2fd272b900842a54f3f862e75ef9.jpg"
                mode="aspectFill"
              />
            </view>
            <view class="message-info">
              <view class="message-title">
                <text class="sender">李四同学</text>
                <text class="time">昨天</text>
              </view>
              <view class="message-content">
                <text>我在图书馆捡到的书包是您的吗？请描述一下书包特征</text>
              </view>
            </view>
            <view class="status-indicator">
              <text class="status-text">已读</text>
            </view>
          </view>
        </view>

        <!-- 系统通知 - 新消息 -->
        <view 
          class="message-card system-notice" 
          @click="goToChat('system_2')"
        >
          <view class="message-header">
            <view class="avatar">
              <image
                src="https://ai-public.mastergo.com/ai/img_res/5473c24c1b6be0c247d81031bdf33e4e.jpg"
                mode="aspectFill"
              />
            </view>
            <view class="message-info">
              <view class="message-title">
                <text class="sender">校园失物招领</text>
                <text class="time">昨天</text>
              </view>
              <view class="message-content">
                <text>您发布的"丢失水杯"信息有新的回复</text>
              </view>
            </view>
            <view class="status-indicator">
              <text class="status-text">已读</text>
            </view>
          </view>
        </view>

        <!-- 用户消息 - 物品确认 -->
        <view 
          class="message-card" 
          @click="goToChat('user_3')"
        >
          <view class="message-header">
            <view class="avatar">
              <image
                src="https://ai-public.mastergo.com/ai/img_res/9f62c99bd6bda6bbb6d364b973ec270c.jpg"
                mode="aspectFill"
              />
            </view>
            <view class="message-info">
              <view class="message-title">
                <text class="sender">王五同学</text>
                <text class="time">前天</text>
              </view>
              <view class="message-content">
                <text>确认是我的书包，请问在哪里可以领取？</text>
              </view>
            </view>
            <view class="status-indicator">
              <text class="status-text">已读</text>
            </view>
          </view>
        </view>
      </view>
    </scroll-view>

    <!-- 空状态 -->
    <view class="empty-state" v-if="messages.length === 0">
      <image src="/static/icons/empty-message.png" mode="aspectFit" class="empty-icon" />
      <text class="empty-text">暂无消息</text>
    </view>
  </view>
</template>

<script lang="ts" setup>
import { ref, onMounted } from 'vue';

// 模拟消息数据
const messages = ref([
  {
    id: 'system_1',
    type: 'system',
    sender: '校园失物招领',
    avatar: 'https://ai-public.mastergo.com/ai/img_res/b7f8ff0264c28a0e73b20aa75d866bb1.jpg',
    content: '您的失物招领信息已通过审核',
    time: '10:30',
    isRead: true,
    badge: '审核'
  },
  {
    id: 'user_1',
    type: 'user',
    sender: '张三同学',
    avatar: 'https://ai-public.mastergo.com/ai/img_res/37998c4797d59d14cf4d1abad6197464.jpg',
    content: '您好，我找到了您丢失的校园卡，请联系我领取',
    time: '09:45',
    isRead: false
  },
  {
    id: 'user_2',
    type: 'user',
    sender: '李四同学',
    avatar: 'https://ai-public.mastergo.com/ai/img_res/db5b2fd272b900842a54f3f862e75ef9.jpg',
    content: '我在图书馆捡到的书包是您的吗？请描述一下书包特征',
    time: '昨天',
    isRead: true
  },
  {
    id: 'system_2',
    type: 'system',
    sender: '校园失物招领',
    avatar: 'https://ai-public.mastergo.com/ai/img_res/5473c24c1b6be0c247d81031bdf33e4e.jpg',
    content: '您发布的"丢失水杯"信息有新的回复',
    time: '昨天',
    isRead: true
  },
  {
    id: 'user_3',
    type: 'user',
    sender: '王五同学',
    avatar: 'https://ai-public.mastergo.com/ai/img_res/9f62c99bd6bda6bbb6d364b973ec270c.jpg',
    content: '确认是我的书包，请问在哪里可以领取？',
    time: '前天',
    isRead: true
  }
]);

// 当前选中的标签
const currentTab = ref('all');

// 标记全部消息为已读
const markAllRead = () => {
  messages.value.forEach(msg => {
    msg.isRead = true;
  });
  
  uni.showToast({
    title: '全部消息已标记为已读',
    icon: 'success',
    duration: 1500
  });
};

// 切换消息分类标签
const switchTab = (tabType: string) => {
  currentTab.value = tabType;
};

// 点击消息进入聊天框
const goToChat = (messageId: string) => {
  // 标记消息为已读
  const message = messages.value.find(msg => msg.id === messageId);
  if (message) {
    message.isRead = true;
  }
  
  // 使用原生导航方法
  uni.navigateTo({
    url: `/components/Communication?messageId=${messageId}`
  });
};

// 页面加载时执行初始化操作
onMounted(() => {
  console.log('消息页面已加载');
  
  // 检查是否有未读消息
  const hasUnread = messages.value.some(msg => !msg.isRead);
  if (hasUnread) {
    uni.setTabBarBadge({
      index: 2, // 假设消息页面是第三个tab
      text: messages.value.filter(msg => !msg.isRead).length.toString()
    });
  } else {
    uni.removeTabBarBadge({
      index: 2
    });
  }
});
</script>

<style>
page {
  height: 100%;
  background-color: #f8fafc;
  font-family: -apple-system, BlinkMacSystemFont, 'Helvetica Neue', Helvetica, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', Arial, sans-serif;
}

.container {
  display: flex;
  flex-direction: column;
  height: 100%;
}

/* 导航栏样式 */
.nav-bar {
  height: 100rpx;
  background: linear-gradient(135deg, #4a90e2, #357abd);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 32rpx;
  box-shadow: 0 2rpx 10rpx rgba(74, 144, 226, 0.2);
  position: relative;
}

.nav-title {
  font-size: 40rpx;
  font-weight: 600;
  color: #ffffff;
  text-align: center;
  position: absolute;
  left: 0;
  right: 0;
}

.nav-action {
  font-size: 28rpx;
  color: rgba(255, 255, 255, 0.9);
  display: flex;
  align-items: center;
  z-index: 1;
}

.action-icon {
  width: 30rpx;
  height: 30rpx;
  margin-right: 12rpx;
}

/* 分类标签样式 */
.tabs {
  display: flex;
  justify-content: center;
  background-color: #ffffff;
  padding: 0 32rpx;
  border-bottom: 1rpx solid #e8f0fb;
  height: 90rpx;
  align-items: center;
}

.tab {
  padding: 20rpx 0;
  margin: 0 32rpx;
  position: relative;
}

.tab text {
  font-size: 32rpx;
  color: #666666;
}

.tab.active text {
  color: #4a90e2;
  font-weight: 500;
}

.tab-indicator {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  height: 6rpx;
  background-color: #4a90e2;
  border-radius: 3rpx;
  transform: scaleX(0.6);
  transition: transform 0.2s ease;
}

.tab.active .tab-indicator {
  transform: scaleX(1);
}

/* 消息列表居中布局核心样式 */
.message-list-container {
  flex: 1;
  overflow: auto;
  padding: 24rpx 0; /* 移除左右边距，通过wrapper居中 */
}

.message-list-wrapper {
  max-width: 750rpx; /* 限制最大宽度 */
  margin: 0 auto; /* 水平居中 */
  padding: 0 32rpx; /* 保留内边距 */
}

.message-card {
  background-color: #ffffff;
  border-radius: 16rpx;
  margin-bottom: 24rpx;
  padding: 24rpx;
  border: 1rpx solid #e8f0fb;
  display: flex;
  flex-direction: column;
  position: relative;
  transition: all 0.2s ease;
  box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.05);
}

.message-card:hover {
  transform: translateY(-2rpx);
  box-shadow: 0 6rpx 16rpx rgba(0, 0, 0, 0.1);
}

.message-card.unread {
  background-color: #f5faff;
  border-left: 6rpx solid #4a90e2;
}

.message-card.system-notice {
  border-left: 6rpx solid #7eb6ff;
}

.message-header {
  display: flex;
  align-items: center;
}

.avatar {
  width: 80rpx;
  height: 80rpx;
  border-radius: 50%;
  overflow: hidden;
  border: 2rpx solid #e3f2fd;
  margin-right: 24rpx;
  flex-shrink: 0;
  box-shadow: 0 2rpx 8rpx rgba(74, 144, 226, 0.1);
}

.avatar image {
  width: 100%;
  height: 100%;
}

.message-info {
  flex: 1;
  min-width: 0;
}

.message-title {
  display: flex;
  justify-content: space-between;
  margin-bottom: 12rpx;
  align-items: center;
}

.sender {
  font-size: 34rpx;
  color: #333333;
  font-weight: 500;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.time {
  font-size: 26rpx;
  color: #999999;
  margin-left: 16rpx;
  white-space: nowrap;
}

.message-content {
  font-size: 30rpx;
  color: #666666;
  line-height: 1.5;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  text-overflow: ellipsis;
}

.badge {
  display: inline-block;
  background-color: #4a90e2;
  color: #ffffff;
  font-size: 22rpx;
  padding: 4rpx 12rpx;
  border-radius: 20rpx;
  margin-left: 16rpx;
  vertical-align: middle;
  margin-top: -4rpx;
}

.status-indicator {
  margin-left: 24rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.status-text {
  font-size: 24rpx;
  color: #999999;
  background-color: #f0f5ff;
  padding: 6rpx 16rpx;
  border-radius: 16rpx;
}

.unread-dot {
  width: 16rpx;
  height: 16rpx;
  background-color: #ff4d4f;
  border-radius: 50%;
  box-shadow: 0 2rpx 4rpx rgba(255, 77, 79, 0.3);
}

/* 空状态样式 */
.empty-state {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 40rpx;
}

.empty-icon {
  width: 200rpx;
  height: 200rpx;
  opacity: 0.2;
  margin-bottom: 32rpx;
}

.empty-text {
  font-size: 32rpx;
  color: #999999;
}
</style>